Sometimes it's just not practical to attach a profiler to a running application. For instance you may want to profile your application's startup code.
If you're using Oracle's JDK you can solve this problem with command line parameters which start Java Flight Recorder as your application starts up. The available parameters are somewhat byzantine so it's worth your time to read the command line documentation
As an example, I use the VM parameters below when I want to profile an integration test from inside IntelliJ:
-XX:+UnlockCommercialFeatures -XX:+FlightRecorder -XX:StartFlightRecording=defaultrecording=true -XX:FlightRecorderOptions=dumponexit=true,dumponexitpath=/Some/Folder
If you use these parameters a .jfr file containing profile data will be written to /Some/Folder when your application exits. You can then open the file with Java Mission Control.
If .jfr file associations aren't set up you can launch Java Mission Control on a Mac from /usr/bin/jmc.
This is what it looks like - not bad for a tool which is free to use!